package com.wt.springsamples.juc;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ThreadLocal
 * InheritableThreadLocal:父子线程数据共享
 * 阿里巴巴的TransmittableThreadLocal:解决线程池复用导致的内存泄露问题
 */
public class ThreadLocalTest {

    public static void main(String[] args) {
//        ThreadLocal<Integer> local = ThreadLocal.withInitial(()->0);
//        local.set(new Integer(0));
//        local = ThreadLocal.withInitial(()->1);
//        local.set(new Integer(1));
//        local = ThreadLocal.withInitial(()->2);
//        local.set(new Integer(2));

//        InheritableThreadLocal inheritableThreadLocal = new InheritableThreadLocal();
//        inheritableThreadLocal.set(new Integer(2));

//        ThreadLocal<Integer> finalLocal = local;
        new Thread("ThreadLocal-Test-Thread"){
            @Override
            public void run(){
                ThreadLocal l = ThreadLocal.withInitial(()->0);
                l.set(new Integer(111));
                ThreadLocal<Integer> local = new ThreadLocal<>();
                local.set(new Integer(1));
                local.set(new Integer(3));
            }
        }.start();
//        new Thread(()->  ThreadLocal.withInitial(()->0).set(new Integer(3)),"thread 1").start();
//        new Thread(()-> System.out.println("inheritableThreadLocal>>>"+inheritableThreadLocal.get())).start();


//        ThreadPoolExecutor executorService  = new ThreadPoolExecutor(5, 10,0L, TimeUnit.MILLISECONDS,
//                new LinkedBlockingQueue<Runnable>(100));

//        try {
//            for(int i=0;i<10;i++){
//
//                executorService.execute(()-> {
//                    inheritableThreadLocal.set(i++);
//                    System.out.println("inheritableThreadLocal》》》"+inheritableThreadLocal.get());
//                });
//            }
//        }finally {
//            executorService.shutdown();
//        }

//        executorService.prestartAllCoreThreads();


//        while (executorService.isShutdown()){
//            executorService.prestartCoreThread();
//            executorService.execute(()-> {
//                System.out.println("restart》》》"+inheritableThreadLocal.get());
//            });
//            break;
//        }
//        System.out.println("主线程："+inheritableThreadLocal.get());
    }
}
